package com.hackordie.controllers;

import java.security.Principal;

import javax.servlet.http.HttpServletRequest;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hackordie.bindables.BindableRole;
import com.hackordie.commons.authorities.Profile;
import com.hackordie.commons.authorities.User;
import com.hackordie.commons.game.player.UserComponent;
import com.hackordie.commons.hardware.components.AComponent;
import com.hackordie.exceptions.HackOrDieException;

@Controller
@RequestMapping(value = "/user")
public class UserController extends AMainController {

	@RequestMapping(value = "/dashboard", method = RequestMethod.GET)
	public String login(ModelMap model, HttpServletRequest request) throws Exception {
		breadCrumbService.addNode("User", 0, request);
		return "user/dashboard";
	}

	@RequestMapping(value = "/profile", method = RequestMethod.GET)
	public String profile(ModelMap model, Principal principal, HttpServletRequest request) throws HackOrDieException {
		breadCrumbService.addNode("Profile", 1, request);
		User user = hodManager.getUser(principal.getName());
		model.addAttribute("active_UserProfile", "active");
		Profile profile = null;
		
		try {
			profile = hodManager.getProfile(user.getId());
		} catch (HackOrDieException e) {
			return "redirect:/profile/create";
		}
				
		model.addAttribute("user", user);
		model.addAttribute("profile", profile);
		return "user/profile";

	}

	@RequestMapping(value = "/profile/create", method = RequestMethod.GET)
	public String profileCreate(ModelMap model, Principal principal, HttpServletRequest request) throws Exception {
		breadCrumbService.addNode("Create Profile", 1, request);
		return "user/create_profile";
	}

	@RequestMapping(value = "/profile/{idUser}", method = RequestMethod.GET)
	public String profileId(@PathVariable("idUser") int idUser, ModelMap model, HttpServletRequest request) throws Exception {
		breadCrumbService.addNode("Profile", 1, request);
		User user = hodManager.getUser(idUser);
		Profile profile = hodManager.getProfile(user.getId());
		
		String auth = SecurityContextHolder.getContext().getAuthentication().getAuthorities().toString();
		
		if(!(auth.contains("ROLE_ADMIN"))) {
		    return "redirect:/user/profile";		    
		}
				
		model.addAttribute(new BindableRole());
		model.addAttribute("roles", hodManager.getAllRoles());	
		model.addAttribute("user", user);
		model.addAttribute("profile", profile);
		model.addAttribute("active_UserProfile", "active");
		return "user/profile";
	}
	
	
	@RequestMapping(value = "/shop", method = RequestMethod.GET)
    public String shop(ModelMap model, HttpServletRequest request) throws Exception {
        breadCrumbService.addNode("Shop", 1, request);  //1: viene desde "user" o "admin"        
        return "redirect:/user/shop/pages/1";
	}
	
	@RequestMapping(value = "/shop/pages/{page}", method = RequestMethod.GET)
    public String shopPages(@PathVariable("page") int page, ModelMap model, HttpServletRequest request) throws Exception {
	    breadCrumbService.addNode("Page " + page, 2, request);
        model.addAttribute("active_Shop", "active");
        
        int PAGE_FIELDS = 10;
        int pages = (int) Math.ceil((double) hodManager.countAllComponents() / (double) PAGE_FIELDS);
        int begin = Math.max(1, page - 5);
        int end = Math.min(begin + 10, pages);

        model.addAttribute("totalPages", pages);
        model.addAttribute("beginIndex", begin);
        model.addAttribute("endIndex", end);
        model.addAttribute("currentIndex", page);

        int first = (page - 1) * PAGE_FIELDS;
        int last = first + (PAGE_FIELDS);
        
        String userName = SecurityContextHolder.getContext().getAuthentication().getName();
                
        User user = hodManager.getUser(userName);
        AComponent[] componentsInShop = hodManager.getAllComponentsByRange(first, last);
        UserComponent[] userComponents   = hodManager.getUserBagComponents(user.getId(), true);
        
        model.addAttribute("user", user);
        model.addAttribute("componentsInSHop", componentsInShop);
        model.addAttribute("userComponents", userComponents);
                
        return "user/shop";
    }

}
